Lab 6

  1. Write a C++ program that asks the user for a name until the user enters END. Print the name each time. When you are done, print "I am done."
  2. Sample run of program:
    Enter a name and I will repeat it back to you. Type END when you wish to quit. > Alex Alex > Kent Kent > Xiuyi Xiuyi > END I am done.

  3. Write a C++ program that keeps asking the user for a number until the user enters a negative number. After each non-negative number entered by the user, print "Even" if the user entered an even number, and print "Odd" otherwise. After the user enters a negative number, print "Goodbye" to the monitor.
  4. Sample run of program:
    Enter a number and I will tell you if it is even or odd. Enter a negative number to stop: > 2 Even > 3 Odd > 7 Odd > -2 Goodbye.

  5. Write a C++ program that calculates how much a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. How many days will it take for the person's daily salary to exceed $10,000? Display a table showing the day number and salary per day in dollars. Use a tab to separate the two columns.
  6. Sample run of program showing first nine lines printed to the monitor. This is not a complete run of the code. Your program should output lines until the salary reaches at least $10,000:
    Day Salary 1 0.01 2 0.02 3 0.04 4 0.08 5 0.16 6 0.32 7 0.64 8 1.28 9 2.56
  7. Write a C++ program that reads a positive integer and prints all of its binary digits (in reverse order). Print the number % 2, then replace the number by number / 2. Keep going until the number is 0.
  8. Validate the input. Do not accept a negative integer.
    Sample run of program:
    Enter a positive integer: > -2 Invalid input! Try again: > 20 Your number in binary in reverse order is: 0 0 1 0 1

  9. Write a C++ program that asks the user for an integer and then prints out all its factors. Recall that if a number x is a factor of another number y, when y is divided by x the remainder is 0.
  10. Validate the input. Do not accept a negative integer.
    Sample run of program:
    Enter a positive integer: > -71 Invalid input! Try again: > 42 The factors of 42 are 1 2 3 6 7 14 21 42